package com.waterfairy.particle;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;

import java.util.List;

/**
 * @author water_fairy
 * @email 995637517@qq.com
 * @date 2020/8/21 17:28
 * @info:
 */
public class ObjectBean {
    private static final String TAG = "ObjectBean";
    public boolean enable = true;

    //G = mg  F=cv
    //( mg - cv ) / m = a
    // v=mg/c
    //阻尼系数=g=9.8 且 速度=质量  匀速
    //距离S=初速度V*时间t+1/2at^2


    public Bitmap bitmap;
    public Matrix matrix = new Matrix();
    private float[] matrixValues = new float[9];

    public int oriLeft;
    public int oriTop;
    public int oriRight;
    public int oriBottom;
    //初始速度
    public float startSpeedX;
    public float startSpeedY;
    //起始时间
    public long startTime;
    //质量
    public float m = 0;
    //力 xy
    public float fX = 9.8F;
    public float fY = fX * 2;

    //规则
    public List<ActionBean> actionBeans;
    //缩放
    private float scale;

    public void init(Bitmap bitmap, int transX, int transY, float scale, float speedX, float speedY) {
        this.startSpeedX = speedX;
        this.startSpeedY = speedY;
        this.bitmap = bitmap;
        this.scale = scale;
        this.startTime = System.currentTimeMillis();
        this.enable = true;
        if (this.bitmap != null && !this.bitmap.isRecycled() && this.bitmap.getHeight() != 0) {
            oriLeft = 0;
            oriTop = 0;
            oriRight = this.bitmap.getWidth();
            oriBottom = this.bitmap.getHeight();
            matrix.setScale(scale, scale);
            matrix.postTranslate(transX, transY);
        }
    }

    @Override
    public String toString() {
        return "FireworksBean{" +
                "left=" + oriLeft +
                ", top=" + oriTop +
                ", right=" + oriRight +
                ", bottom=" + oriBottom +
                ", speed=" + startSpeedX +
                ", bitmap=" + (bitmap == null || bitmap.isRecycled() ? "null" : bitmap.getWidth() + "-" + bitmap.getHeight()) +
                '}';
    }


    public void trans(int dx, int dy) {
        if (matrix != null) {
            matrix.getValues(matrixValues);
        }
    }


    public void update() {
        rectF();
        float dTime = (System.currentTimeMillis() - startTime) / 1000F;
//          x=vt+1/2at`2
        int dx = (int) (startSpeedX * dTime + (m == 0 ? 0 : (0.5F * (fX / m) * (dTime * dTime))));
        int dy = (int) (startSpeedY * dTime + (m == 0 ? 0 : (0.5F * (fY / m) * (dTime * dTime))));
        matrix.postTranslate(dx, dy);
        startTime = System.currentTimeMillis();
        matrix.postRotate(0.5F, rectF.centerX(), rectF.centerY());
    }

    private RectF rectF;
    private Rect rect;

    public RectF getRectF() {
        if (rectF == null) return new RectF();
        return rectF;
    }

    public Rect getRect() {
        if (rect == null) return new Rect();
        return rect;
    }

    public RectF rectF() {
        if (rectF == null) {
            rectF = new RectF();
        }
        if (rect == null) {
            rect = new Rect();
        }
        rectF.set(oriLeft, oriTop, oriRight, oriBottom);
        matrix.mapRect(rectF);
        rect.set((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom);
        return rectF;
    }

    public Rect getBitmapRectF(Rect rectBitmap) {
        if (rectBitmap != null) {
            rectBitmap.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
        }
        return rectBitmap;
    }

    public void onDraw(Canvas canvas) {
        if (bitmap != null && !bitmap.isRecycled() && enable) {
            canvas.drawBitmap(bitmap, matrix, null);
        }
    }
}
